package org.csu.mypetstore.controller;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.csu.mypetstore.domain.Item;
import org.csu.mypetstore.domain.Order;
import org.csu.mypetstore.domain.Product;
import org.csu.mypetstore.service.CategoryService;
import org.csu.mypetstore.service.ItemService;
import org.csu.mypetstore.service.ProductService;
import org.csu.mypetstore.tools.Base64ToImage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.PrintWriter;
import java.util.*;

@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private ItemService itemService;

    @GetMapping
    @ResponseBody
    public String get(String type,String keyword){
        if(type.equals("complete"))
        {
            List<Product> products = productService.searchProductList(keyword);
            ArrayList<String> nameList = new ArrayList<>();
            for (Product p : products) {
                nameList.add(p.getName());
            }
            System.out.println(nameList);
            JSONObject object = new JSONObject();
            object.put("status","OK");
            object.put("msg","商品信息获取成功!");
            object.put("data",nameList);
            return object.toString();
        }
        else if(type.equals("search"))
        {
            List<Product> products = productService.searchProductList(keyword);
            System.out.println(products);
            JSONObject object = new JSONObject();
            object.put("status","OK");
            object.put("msg","商品信息获取成功!");
            object.put("data",products);
            return object.toString();
        }
        else if(type.equals("getProductListByCategoryId"))
        {
            System.out.println(keyword);
            List<Product> products = productService.getProductListByCategory(keyword);
            System.out.println(products);
            JSONObject object = new JSONObject();
            object.put("status","OK");
            object.put("msg","商品信息获取成功!");
            object.put("data",products);
            return object.toString();
        }
        else{
            if(keyword.isEmpty())
            {
                JSONObject object = new JSONObject();
                object.put("status","NOK");
                object.put("msg","商品信息获取失败!");
                return object.toString();
            }
            else
            {
                Product product = productService.getProduct(keyword);
                JSONObject object = new JSONObject();
                object.put("status","OK");
                object.put("msg","商品信息获取成功");
                object.put("data",product);
                System.out.println(object);
                return object.toString();
            }

        }
    }


    @PostMapping
    @ResponseBody
    public String add(String productId, String category, String name, String description,String picture) {
        String filename =  Long.toString(System.currentTimeMillis());
        String filePath = "src/main/resources/static/"+filename+".png";
        Base64ToImage.Base64ToImage(picture,filePath);
        Product product = new Product();
        product.setCategoryId(category);
        product.setProductId(productId);
        product.setName(name);
        product.setDescription(description);
        product.setPicture(filename+".png");
        JSONObject object = new JSONObject();
        String s = productService.addProduct(product);
        if(s.equals("Succeed in Adding Product"))
        {
            object.put("status","OK");
            s = "商品添加成功!";
        }
        else
        {
            object.put("status","NOK");
            s = "商品添加失败!";
        }
        object.put("msg",s);
        return object.toString();
    }

    @DeleteMapping
    @ResponseBody
    public String delete(String productId){
        System.out.println(productId);
        JSONObject object = new JSONObject();
        itemService.deleteItemByProductId(productId);
        String s = productService.deleteProduct(productId);
        if(s.equals("Succeed in Deleting Product"))
        {
            object.put("status","OK");
            s = "删除商品成功!";
        }
        else
        {
            object.put("status","NOK");
            s = "删除商品失败!";
        }
        object.put("msg",s);
        return object.toString();
    }

    @PutMapping
    @ResponseBody
    public String update(String productId,String category,String name,String description)
    {
        Product product = new Product();
        product.setCategoryId(category);
        product.setProductId(productId);
        product.setName(name);
        product.setDescription(description);
        JSONObject object = new JSONObject();
        String s = productService.updateProduct(product);
        if(s.equals("Succeed in Updating Product"))
        {
            object.put("status","OK");
            s = "更新商品成功!";
        }
        else
        {
            object.put("status","NOK");
            s = "更新商品失败!";
        }
        object.put("msg",s);
        return object.toString();
    }
}
